<?php
namespace app\app\controller;
use think\Db;
class Shop extends Common
{
    //获取商品分类及商品列表
    public function list_all()
    {
        $uid = token_decode(req('token'));
        $seller_id = req('seller_id');
        $instore = req('instore');
				$instore = $instore?$instore:1;
        $state = 1;
        $data = [];
        $msg = '获取成功';
        if (empty($seller_id)) {
            $state = 0;
            $msg = '请选择商家';
        } else {
            //商家分类
            $category = Db::table('service_category')->field('id as cate_id,title,image')->where('uid',$seller_id)->order('id asc')->select();
            $data = [];
            //商品列表，购物车
            $fields = 'id as goods_id,volume_date,volume as month_sale,discount,title,image1,image2,image3,image4,service,content,price,present,required';
            foreach ($category as $k => $v){
                $where['cate_id'] = $v['cate_id'];
                $goods = Db::table('service')->field($fields)->where(['uid'=>$seller_id,'cate_id'=>$v['cate_id'],'status'=>1,'instore'=>$instore])->order('id desc')->select();
                if(count($goods) > 0){
                    foreach ($goods as $k2 => $v2){
                        if($v2['volume_date'] != date('Ym')){
                            Db::table('service')->where('id',$v2['goods_id'])->update(['volume'=>0,'volume_date'=>date('Ym')]);
                        }
						$goods[$k2]['original_price'] = $goods[$k2]['price'];
                        if(!empty((float)$v2['discount'])){
                            $goods[$k2]['price'] = (string)($v2['price'] * ($v2['discount']/10));
                        }
                        $cart = Db::table('cart')->where(['seller_id'=>$seller_id,'goods_id'=>$v2['goods_id'],'uid'=>$uid,'order_id'=>'','instore'=>$instore])->find();
                        $goods[$k2]['cart_number'] = (int)$cart['number'];
                        $goods[$k2]['spec'] = Db::table('service_spec')->where(['service_id'=>$v2['goods_id']])->select();
                    }
                    $category[$k]['goods'] = $goods;
                    $data[] = $category[$k];
                }
            }
        }
        return json(['state' => $state, 'data' => $data, 'msg' => $msg]);
    }

    //搜索
    public function search()
    {
        $uid = token_decode(req('token'));
        $input = req('input');
        $lng = req('lng');
        $lat = req('lat');
        $queue = req('queue')?req('queue'):'nearby';
        $state = 1;
        $data = [];
        if(empty($input)){
            $state = 0;
            $msg = '请输入搜索内容';
        }else{
            $order = '';
            if($queue == 'nearby'){
                $order = 'ACOS(SIN(('.$lat.' * 3.1415) / 180 ) *SIN((lat * 3.1415) / 180 ) +COS(('.$lat.' * 3.1415) / 180 ) * COS((lat * 3.1415) / 180 ) *COS(('.$lng.'* 3.1415) / 180 - (lng * 3.1415) / 180 ) ) * 6380 asc,business desc';
            }elseif($queue == 'sales'){
                $order = 'volume desc';
            }
            $goods_array = Db::table('service')->field('uid')->where('title','like','%'.$input.'%')->where('status',1)->order('id desc')->select();
			// var_dump(Db::table('service')->getLastSql());exit;
            $seller_id_str = '';
            foreach ($goods_array as $k => $v){
                $seller_id_str .= $v['uid'].',';
            }
            $seller_id_str = substr($seller_id_str,0,strlen($seller_id_str)-1);
            $data = Db::table('seller')->field('id as seller_id,aid,volume_date,headimg,name,min_express_charge,lng,lat,new_user_reduction,volume as month_sale,label,business,introduce,phone,address,advance,present')->whereIn('id',$seller_id_str)->whereOr('name','like','%'.$input.'%')->order($order)->select();
            foreach (array_reverse($data) as $k => $v){
                $evaluate_count = Db::table('evaluate')->where('seller_id', $v['seller_id'])->where('time>=complete_time or complete_time<='.time()-300)->count();
                $evaluate = 0;
                $evaluate_array = Db::table('evaluate')->where('seller_id', $v['seller_id'])->where('time>=complete_time or complete_time<='.time()-300)->select();
                foreach ($evaluate_array as $index => $value) {
                    $evaluate += $value['star'];
                }
                if ($evaluate_count != 0) {
                    $data[$k]['grade'] = (string)(round($evaluate / $evaluate_count, 1));
                } else {
                    $data[$k]['grade'] = '';
                }
                if($v['volume_date'] != date('Ym')){
                    Db::table('seller')->where('id',$v['seller_id'])->update(['volume'=>0,'volume_date'=>date('Ym')]);
                }
                $data[$k]['month_sale'] = (int)$v['month_sale'];
                $data[$k]['distance'] = (string)($this->getDistance($lat,$lng,$v['lat'],$v['lng'])/1000);  //距离

                $express_config = Db::table('express_config')->where('aid',$v['aid'])->value('charge');
				//计算配送费
				$pi = $data[$k]['distance'];
                $express_charge = $express_config['charge'];
				if($pi > $express_config['scope']){
					$express_charge += $express_config['add_charge']*($pi-$express_config['scope']);
				}
				if ($express_charge < $express_config['charge']) {
					$express_charge = $express_config['charge'];
				}
				$data[$k]['express_charge'] = (string)round($express_charge);
                unset($data[$k]['lng']);
                unset($data[$k]['lat']);
                $money_off = Db::table('money_off')->where('seller_id',$v['seller_id'])->select();
                $string = '';
                foreach ($money_off as $k2 => $v2){
                    $string .= '满'.$v2['full'].'减'.$v2['minus'].';';
                }
                if(!empty($v['new_user_reduction'])){
                    $data[$k]['new_user_reduction'] = '新用户下单立减'.$v['new_user_reduction'];
                }
                $data[$k]['money_off'] = (string)$string;
                $data[$k]['isFavourite'] = (int)Db::table('favourite')->where(['uid' => $uid, 'seller_id' => $v['seller_id']])->count();
                $search_goods = (array)Db::table('service')->field('id as goods_id,discount,volume as month_sale,volume_date,title,image1,image2,image3,image4,service,content,present')->where('title','like','%'.$input.'%')->where(['uid'=>$v['seller_id'],'instore'=>$instore])->where('status',1)->select();
                foreach ($search_goods as $k2 => $v2){
                    if($v2['volume_date'] != date('Ym')){
                        Db::table('service')->where('id',$v2['goods_id'])->update(['volume'=>0,'volume_date'=>date('Ym')]);
                    }
                    if(!empty((float)$v2['discount'])){
                        $search_goods[$k2]['price'] = (string)($v2['price'] * ($v2['discount']/10));
                    }
                }
                $data[$k]['goods'] = $search_goods;
            }
            $msg = '获取成功';
        }
        return json(['state'=>$state,'data'=>$data,'msg'=>$msg]);
    }

}
